﻿using DTM.Model;
using DTM.Repository;
using FluentValidation;
using System.Collections.Generic;

namespace DTM.Domain.Players
{
    /// <summary>
    /// Class <see cref="PlayerService" /> declare player service class.
    /// </summary>
    public class PlayerService : ServiceBase<IPlayer>, IPlayerService
    {
        /// <summary>
        /// Class <see cref="UserService" /> constructor
        /// </summary>
        /// <param name="unitOfWorks">The unit of works.</param>
        /// <param name="validator">The validator.</param>

        #region Constructors

        public PlayerService(IUnitOfWorkProvider unitOfWorks, IValidator<IPlayer> validator) : base(unitOfWorks, validator)
        {
        }

        #endregion Constructors

        #region Methods

        /// <summary>
        /// Inserts new player.
        /// </summary>
        /// <param name="player">Player to insert.</param>
        /// <returns></returns>
        public IPlayer Insert(IPlayer player)
        {
            Validate(player);
            using (var unitOfWork = GetUnitOfWork())
            {
                var entity = unitOfWork.Players.Insert(player);
                unitOfWork.Commit();
                return entity;
            }
        }

        /// <summary>
        /// Returns all players.
        /// </summary>
        /// <returns></returns>
        public IEnumerable<IPlayer> GetAll()
        {
            using (var unitOfWork = GetUnitOfWork())
            {
                var result = unitOfWork.Players.GetAll();
                return result;
            }
        }

        /// <summary>
        /// Update the player.
        /// </summary>
        /// <param name="player">Player to update.</param>
        public void Update(IPlayer player)
        {
            Validate(player);
            using (var unitOfWork = GetUnitOfWork())
            {
                unitOfWork.Players.Update(player);
                unitOfWork.Commit();
            }
        }

        /// <summary>
        /// Delete the player.
        /// </summary>
        /// <param name="playerId">Player to delete.</param>
        public void Delete(int playerId)
        {
            using (var unitOfWork = GetUnitOfWork())
            {
                unitOfWork.Players.Delete(playerId);
                unitOfWork.Commit();
            }
        }

        #endregion Methods
    }
}